package {
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.utils.getDefinitionByName;

	import com.passioneffect.components.Tooltip;
	import com.passioneffect.contentmcs.AbstractContentMovieClip;
	import com.passioneffect.display.Component;
	import com.passioneffect.display.ImageLoader;
	import com.passioneffect.display.VideoPlayer;
	import com.passioneffect.events.PEEvent;
	import com.passioneffect.widgets.CirclePreloader;
	import com.passioneffect.widgets.RectGrow;
	import com.passioneffect.widgets.Reflection;

	import gs.TweenMax;	

	/**
	 * @author Passion Effect
	 */
	public class CarouselContent extends AbstractContentMovieClip {
		private var contentLoader : MovieClip;
		private var comp : SuperCarouselV2;
		private var reflection : Reflection;

		private var rect1 : RectGrow;
		private var rect2 : RectGrow;
		private var rect3 : RectGrow;

		private var info : Object;
		private var infoLock : Boolean;
		private var reflectionUpdateTime : Number = 0;

		private var ico : MovieClip;

		public function CarouselContent() {
			super();
		}

		public function onContentData(data : Object) : void {
			this.data = data;
			comp = SuperCarouselV2(component);
			
			if(data.image) {
				contentLoader = new ImageLoader(new CirclePreloader(), comp.thumbnailWidth, comp.thumbnailHeight);
				contentLoader.autoScale = true;
				contentLoader.load(data.image, onLoaded);
				addChild(contentLoader);
			}else if(data.video) {
				contentLoader = new VideoPlayer(comp.thumbnailWidth, comp.thumbnailWidth);
				reflectionUpdateTime = 50;
				contentLoader.load(data.video, onLoaded);
				addChild(contentLoader);
			}
			contentLoader.visible = false;
			
			if(data.link) {
				this.buttonMode = true;
			}
			
			setEvents();
			
		}

		private function setEvents() : void {
			this.mouseEnabled = false;
			contentLoader.addEventListener(MouseEvent.MOUSE_UP, clickHandler);
			//set event to contentLoader because these is a reflection
			contentLoader.addEventListener(MouseEvent.ROLL_OVER, overHandler);
			contentLoader.addEventListener(MouseEvent.ROLL_OUT, outHandler);
			component.addEventListener(PEEvent.MOUSE_OVER_ITEM, openHandler);
			component.addEventListener(PEEvent.MOUSE_OUT_ITEM, closeHandler);
		}

		private function closeHandler(event : PEEvent) : void {
			if(comp.type == "carousel") {
				infoLock = false;
				TweenMax.to(this, 0.3, {colorMatrixFilter:{brightness:info.brightness}});
			}
		}

		private function openHandler(event : PEEvent) : void {
			if(comp.type == "carousel") {
				if(!isOver() && !selected) {
					infoLock = true;
					TweenMax.to(this, 0.3, {colorMatrixFilter:{brightness:comp.highlightDarken}});
				}
			}
		}

		private function outHandler(event : MouseEvent) : void {
			//if(comp.type == "carousel") {
			out();
			infoLock = false;
			TweenMax.to(this, 0.3, {colorMatrixFilter:{brightness:info.brightness}, blurFilter:{blurX:info.blur, blurY:info.blur}});
			
			if(comp.border){
				rect1.close();
				rect2.close();
				rect3.close();
			}
			
			if(ico) {
				removeChild(ico);
				ico = null;
			}
		}

		private function overHandler(event : MouseEvent) : void {
			over();
			if(rect1 == null)return;
			rect1.open();
			rect2.open();
			rect3.open();
			infoLock = true;
			
			TweenMax.to(this, 0.3, {colorMatrixFilter:{brightness:1}, blurFilter:{blurX:0, blurY:0}});
			
			updateIco();
			
			if(data.tooltip) {
				Tooltip.show(data.tooltip, new Rectangle(0, 0, contentLoader.width, contentLoader.height), this, comp.tooltipStyle);
			}
		}
		
		private function updateIco() : void {
			if(!selected)return;
			if(contentLoader is VideoPlayer) {
				if(ico) {
					removeChild(ico);
					ico = null;
				}
				if(VideoPlayer(contentLoader).paused) {
					ico = getAsset("Play_ico");
					addChild(ico);
					ico.x = contentLoader.width / 2 - ico.width / 2;
					ico.y = contentLoader.height / 2 - ico.height / 2;
					ico.mouseEnabled = false;
				}else {
					ico = getAsset("Pause_ico");
					addChild(ico);
					ico.x = contentLoader.width / 2 - ico.width / 2;
					ico.y = contentLoader.height / 2 - ico.height / 2;
					ico.mouseEnabled = false;
				}
			}
		}
		
		private function getAsset(name : String) : * {
			var def = getDefinitionByName(name);
			return new def();
		}

		private function clickHandler(event : MouseEvent) : void {
			if(comp.dragging)return;
			if(!selected) {
				scrollToItem();
				select();
			}else {
				if(data.video) {
					contentLoader.resumeOrPause();
					updateIco();
				}
			}
		}

		public function onSelect() : void {
			
			if(component.customSkin)component.addEventListener(PEEvent.FINISH_SCROLL_TO_ITEM, openDesc);
			if(data.link)component.addEventListener(PEEvent.FINISH_SCROLL_TO_ITEM, linkTo);
		}

		private function linkTo(event : PEEvent) : void {
			component.removeEventListener(PEEvent.FINISH_SCROLL_TO_ITEM, linkTo);
			navigateToURL(new URLRequest(data.link), data["link@type"]);
		}

		private function openDesc(event : PEEvent = null) : void {
			updateIco();
		}

		public function onUnselect() : void {
			component.removeEventListener(PEEvent.FINISH_SCROLL_TO_ITEM, openDesc);
			
			if(data.video && !VideoPlayer(contentLoader).paused) {
				VideoPlayer(contentLoader).pause();
			}
			if(ico) {
				removeChild(ico);
				ico = null;
			}
		}

		private function onLoaded() : void {
			setConstraint(contentLoader.width, contentLoader.height);
			if(selected)scrollToItem();
			
			if(comp.reflection) {
				reflection = new Reflection(contentLoader, comp.reflectionAlpha, comp.reflectionRatio, comp.reflectionDistance, reflectionUpdateTime);
				reflection.open();
			}
			
			if(comp.border){
				rect1 = new RectGrow(contentLoader, -8, 0x0000000, 0.5);
				rect2 = new RectGrow(contentLoader, -4, comp.mouseOverBorderColor, 1);
				rect3 = new RectGrow(contentLoader, -1, 0x000000, 1);
			}
			
		}

		public function changeSetting(attName : String,value : *) : void {
			if(attName == "reflection") {
				if(reflection && value == false) {
					reflection.close();
				}else if(value) {
					if(!reflection)reflection = new Reflection(contentLoader, comp.reflectionAlpha, comp.reflectionRatio, comp.reflectionDistance);
					reflection.open();
				}
			}
		}

		public function onInterValue(info : Object) : void {
			//visible=true;

			if(comp.type == "carousel") {
				if(info.scale) {
					contentLoader.scaleX = contentLoader.scaleY = info.scale;
					if(!infoLock || comp.dragging)TweenMax.to(this, 0.3, {colorMatrixFilter:{brightness:info.brightness}, blurFilter:{blurX:info.blur, blurY:info.blur}});
				}
				
				//this.x = info.x - contentLoader.width / 2;
				//this.y=info.y-contentLoader.height/2;	
				//this.y = info.y - contentLoader.height + comp.thumbnailHeight / 2;
				this.x = info.x - comp.thumbnailAlignX * contentLoader.width;
				this.y = info.y - comp.thumbnailHeight / 2 - comp.thumbnailAlignY * (contentLoader.height - comp.thumbnailHeight);
				
				if(reflection)reflection.update();
				if(rect1)rect1.draw();
				if(rect2)rect2.draw();
				if(rect3)rect3.draw();
				
				contentLoader.visible = true;
			}else if(type == "flow") {
				this.x = info.x;
				this.y = info.y;
			}
			this.info = info;
		}
	}
}
